# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgbase=nickel
pkgname=(
  'nickel'
  'nickel-docs'
  'nickel-language-server'
)
pkgver=1.2.1
pkgrel=2
pkgdesc='A generic configuration language'
arch=('loong64' 'x86_64')
url='https://nickel-lang.org'
license=('MIT')
depends=('glibc' 'gcc-libs')
makedepends=('git' 'rust')
checkdepends=('python')
options=('!lto')
_commit='90180def12fca5cb3582d4e90a3a1fe26632818d'
source=("$pkgbase::git+https://github.com/tweag/nickel#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgbase"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgbase"

  # download dependencies
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd "$pkgbase"

  # binaries
  cargo build --frozen --release --all-features

  # generate shell completions
  pushd target/release
  for shell in bash fish zsh; do
    ./nickel gen-completions "$shell" > "$shell-completion"
  done
  popd
}

check() {
  cd "$pkgbase"

  cargo test --frozen --all-features
}

package_nickel() {
  optdepends=(
    'nickel-docs: documentation'
    'nickel-language-server: language server'
  )
  cd "$pkgbase"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" target/release/nickel

  # shell completions
  pushd target/release
  install -vDm644 bash-completion "$pkgdir/usr/share/bash-completion/completions/nickel"
  install -vDm644 fish-completion "$pkgdir/usr/share/fish/vendor_completions.d/nickel.fish"
  install -vDm644 zsh-completion "$pkgdir/usr/share/zsh/site-functions/_nickel"
  popd

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}

package_nickel-docs() {
  pkgdesc='Documentation & examples for the Nickel Programming Language'
  depends=()
  cd "$pkgbase"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase" ./*.md
  cp -vr examples doc/manual "$pkgdir/usr/share/doc/$pkgbase"

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}

package_nickel-language-server() {
  pkgdesc='A language server for the Nickel Programming Language'
  cd "$pkgbase"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" target/release/nls

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
